package io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class IOTest1 {
    public static void main(String[] args) throws IOException {
        String s = "/Users/jiangyuzhong/Documents/tmp2/out.log";
//        OutputStream o = new FileOutputStream(s);
        InputStream i = new FileInputStream(s);

        byte[] b = new byte[100];
        i.read(b);
        System.out.println(new String(b));
        System.out.println("end");
    }

    /**
     * try-with-resources
     *
     * @param zipFileName
     * @param outputFileName
     * @throws java.io.IOException
     */
    public static void writeToFileZipFileContents(String zipFileName,
                                                  String outputFileName)
            throws java.io.IOException {

        java.nio.charset.Charset charset =
                java.nio.charset.StandardCharsets.US_ASCII;
        java.nio.file.Path outputFilePath =
                java.nio.file.Paths.get(outputFileName);

        // Open zip file and create output file with
        // try-with-resources statement

        // try 后跟小括号,里面只能写AutoCloseable对象
        try (
                java.util.zip.ZipFile zf =
                        new java.util.zip.ZipFile(zipFileName);
                java.io.BufferedWriter writer =
                        java.nio.file.Files.newBufferedWriter(outputFilePath, charset);
        ) {
            // Enumerate each entry
            for (java.util.Enumeration entries =
                 zf.entries(); entries.hasMoreElements(); ) {
                // Get the entry name and write it to the output file
                String newLine = System.getProperty("line.separator");
                String zipEntryName =
                        ((java.util.zip.ZipEntry) entries.nextElement()).getName() +
                                newLine;
                writer.write(zipEntryName, 0, zipEntryName.length());
            }
        }
    }

}
